

//原生js封装的文档加载事件
document.ready(function () {



    let captchaCode = '';//保存验证码
    function setCaptcha() {
        //引入验证码
        let captcha2 = new CaptchaMini({
            lineNum: 10,       //线条数量
            fontSize: 50,           //字体大小
        });
        captcha2.draw(document.querySelector('#captcha2'), r => {
            //console.log(r, '验证码2');
            captchaCode = r
        });
    }
    setCaptcha()





    //验证表单
    //声明
    let Text = document.querySelector('.Text')
    let code = document.querySelector('.code')
    let password = document.querySelector('.password')
    let topassword = document.querySelector('.topassword')
    let hint = document.querySelector('.hint')
    let btn = document.querySelector('.btn')




    //点击btn验证
    btn.addEventListener('click', function () {
        //判断

        // console.log(Text.value);

        if (!(/^1[3-9]\d{9}$/.test(Text.value))) {
            hint.textContent = '手机号输入错误，请稍后重试'
        } else if (code.value.toLowerCase() != captchaCode.toLowerCase()) {
            hint.textContent = '验证码有误'
        } else if (!password.value) {
            hint.textContent = '请输入密码'
        } else if (topassword.value != password.value) {
            hint.textContent = '两次密码不一致'
        } else {


            //ajax发送请求
            $http.post('/users/add', { account: Text.value, password: password.value }, function (res) {
                //判断
                if (res.status === 0) {
                    alert('注册成功')

                    location.href = "../page/enter.html"
                } else {
                    alert('注册失败')
                }

                //都正确    
                hint.textContent = ''

            })
        }
    })

    //回车键跳转
    topassword.addEventListener('keyup', function (e) {
        if (e.keyCode === 13) {
            if (!(/^1[3-9]\d{9}$/.test(Text.value))) {
                hint.textContent = '手机号输入错误，请稍后重试'
            } else if (code.value.toLowerCase() != captchaCode.toLowerCase()) {
                hint.textContent = '验证码有误'
            } else if (!password.value) {
                hint.textContent = '请输入密码'
            } else if (topassword.value != password.value) {
                hint.textContent = '两次密码不一致'
            } else {

                //ajax发送请求
                $http.post('/users/add', { account: Text.value, password: password.value }, function (res) {
                    //判断
                    if (res.status === 0) {
                        alert('注册成功')

                        location.href = "../page/enter.html"
                    } else {
                        alert('注册失败')
                    }

                    //都正确    
                    hint.textContent = ''

                })
            }
        }
    })


})

